package com.example.sort.controller;

import com.example.sort.constant.SortConstant;
import com.example.sort.entity.RectangleInfo;
import com.example.sort.result.Result;
import com.example.sort.service.ISortService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/sort")
@Api(value = "排序接口", tags = {"排序接口"})
public class SortController {
    @Autowired
    private ISortService sortService;

    /**
     * 获取随机数量
     * @param count
     * @return
     */
    @GetMapping("/getRandNode")
    @ApiOperationSort(1)
    @ApiOperation(value = "1.获取指定数量的矩形数据",position =1)
    public Result getRandNode(@RequestParam Integer count){
        List<RectangleInfo> list=new ArrayList<>();
        for (Integer i = 0; i < count; i++) {
            list.add(new RectangleInfo((float)(int)(Math.random()*200+20),(float)(int)(Math.random()*100+20)));
        }
        return Result.success(list);
    }

    @PostMapping("/sortLsit")
    @ApiOperationSort(2)
    @ApiOperation(value = "2.排序，只要矩形长宽即可",position =2)
    public Result sortList(@RequestBody List<RectangleInfo> list){
        sortService.sortList(list);
        return Result.success(list,getMsg(list));
    }

    @PostMapping("/gaSort")
    @ApiOperationSort(3)
    @ApiOperation(value = "3.算法排序排序",position =3)
    public Result gaSort(@RequestBody List<RectangleInfo> list){
        List<RectangleInfo> result=sortService.gaSort(list);
        return Result.success(result,getMsg(result));
    }
    /**
     * 获取返回结果
     * @param list
     * @return
     */
    private String getMsg(List<RectangleInfo> list){
        Double areaUsing = list.stream().filter(x -> x.getIsOrder().equals(true)).mapToDouble(x->(x.getWidth()*x.getLength())).sum();
        Long count=list.stream().filter(x->x.getIsOrder()==true).count();
        Integer allCount=list.size();
        return "面积利用率达到"+(areaUsing*100/(SortConstant.width*SortConstant.length))+"%,总共"+allCount+"个矩形，排序了"+count+"个。";
    }
}
